#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <linux/videodev.h>
#include <mntent.h>
#include <sys/vfs.h>
#include <fcntl.h>
#include <math.h>
#include <sys/mman.h>
#include <linux/rtc.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <linux/i2c.h>

#include "clilib.h"


#include "imxdriver.h"



static pthread_t sig_thread_id;
static sigset_t mask;

int cli_advi2c(parse_t * pars_p, char *result);

void *sig_thread(void *arg)
{
	int signo;
	printf("Enter signal monitor thread.\n");
	if (sigwait(&mask, &signo) != 0) {
		printf("sigwait error\n");
	}

	switch (signo) {
	case SIGINT:
		printf("interrupt: SIGINT.\n");
		// close the library

		break;
	default:
		printf("unexpected signal %d\n", signo);
	}
	printf("pthread_exit SIGINT.\n\r");
    pthread_exit((void*) 0);
    return 0;
}

int cli_gpio(parse_t * pars_p, char *result){
	int gpio=0,val=0;
	int fd;
	int ret=0;
	GPIOVAL_t gpioval={0};
	
	
	cget_integer(pars_p,gpio,&gpio);
	cget_integer(pars_p,-1,&val);

	fd = open(DEVICE_NAME,O_RDWR);
	if(fd <=0){
		print("can't open fd %d \n\r",fd);
		return 0;
	}
	
	gpioval.gpio = gpio;
	if(val != -1)
		gpioval.val  = val;
	
	if(val ==-1){
		ret =ioctl(fd, DRIVER_gpio_get_value, &gpioval);	
		printf("0x%x= gpio_read(%d)\n\r",gpioval.val,gpioval.gpio);
	}
	else{
		ret =ioctl(fd, DRIVER_gpio_set_value, &gpioval);	
		printf("gpio_write(%d)= 0x%x\n\r",gpioval.gpio,gpioval.val);
	}
	close(fd);
	
	return 0;
}

int cli_gpiodir(parse_t * pars_p, char *result){
	int gpio=0,val=0,dir=0;
	int fd;
	int ret=0;
	GPIOVAL_t gpioval={0};
	
	
	cget_integer(pars_p,gpio,&gpio);
	cget_integer(pars_p,0,&dir);		
	cget_integer(pars_p,0,&val);

	
	fd = open(DEVICE_NAME,O_RDWR);
	if(fd <=0){
		print("can't open fd %d \n\r",fd);
		return 0;
	}
	
	gpioval.gpio = gpio;
	gpioval.val  = val;
	gpioval.dir  = dir&1; 

	ret =ioctl(fd, DRIVER_gpio_dir, &gpioval);	
	printf("gpio(%d) dir(%d) val(0x%x)\n\r",gpioval.gpio,gpioval.dir,gpioval.val);

	close(fd);
	
	return 0;
}



int cli_gpio_list(parse_t * pars_p, char *result){
	int start=0,count=32;
	int i;
	int fd;
	int ret=0;
	GPIOVAL_t gpioval={0};
	
	
	cget_integer(pars_p,start,&start);
	cget_integer(pars_p,count,&count);
	
	fd = open(DEVICE_NAME,O_RDWR);
	if(fd <=0){
		print("can't open fd %d \n\r",fd);
		return 0;
	}
	for(i=0;i<count;i++){
		gpioval.gpio = start +i;
		gpioval.val  = 0;
		ret =ioctl(fd, DRIVER_gpio_get_value, &gpioval);	
		printf("0x%x= gpio_read(%d)\n\r",gpioval.val,gpioval.gpio);
	}
	
	close(fd);
	
	return 0;
}





int cli_i2ctest(parse_t * pars_p, char *result){
    int fd; 
	int ret;
	I2CVAL_t i2cval={0};
	int reg;
	int val;
	fd = open(DEVICE_NAME,O_RDWR);
	if(fd <=0){
		print("can't open fd %d \n\r",fd);
		return 0;
	}
	cget_integer(pars_p,1,(int*)&i2cval.adapter);
	cget_integer(pars_p,0x3C,(int*)&i2cval.addr);
	cget_integer(pars_p,0x0,(int*)&reg);
	cget_integer(pars_p,-1,(int*)&val);
	
	if(reg >255){
		i2cval.buf[i2cval.count++] = reg>>8;
		i2cval.buf[i2cval.count++] = reg&0xff;
	}
	else{
		i2cval.buf[i2cval.count++] = reg&0xff;
	}
	
	if(val != -1){
		i2cval.buf[i2cval.count++] = val&0xff;
	}
	ret =ioctl(fd, DRIVER_I2C_IO,&i2cval);	
//	printf("%d= Transmit adapter(%d),addr(0x%x),reg(0x%x) val(0x%x)\n\r",i2cval.adapter,i2cval.addr,reg,val);
	
	if(val ==-1){
		i2cval.flags = I2C_M_RD;
		i2cval.count = 1;
		ret =ioctl(fd, DRIVER_I2C_IO,&i2cval);	
		printf("%d= rx adapter(%d),addr(0x%x),reg(0x%x) val(0x%x)\n\r",ret,i2cval.adapter,i2cval.addr,reg,(int)i2cval.buf[0]&0xff);
	}
	else {
		printf("%d= tx adapter(%d),addr(0x%x),reg(0x%x) val(0x%x)\n\r",ret,i2cval.adapter,i2cval.addr,reg,val);
	}
	close(fd);	
	return 0;
}

int cli_i2cloop(parse_t * pars_p, char *result){
    int fd; 
	int i;
	int ret;
	I2CVAL_t i2cval={0};
	int reg;
	int val;
	int loop=0;
	fd = open(DEVICE_NAME,O_RDWR);
	if(fd <=0){
		print("can't open fd %d \n\r",fd);
		return 0;
	}
	
	cget_integer(pars_p,1000,(int*)&loop);
	cget_integer(pars_p,1,(int*)&i2cval.adapter);
	cget_integer(pars_p,0x3C,(int*)&i2cval.addr);
	cget_integer(pars_p,0x0,(int*)&reg);
	cget_integer(pars_p,-1,(int*)&val);
	if(reg >255){
		i2cval.buf[i2cval.count++] = reg>>8;
		i2cval.buf[i2cval.count++] = reg&0xff;
	}
	else{
		i2cval.buf[i2cval.count++] = reg;
	}
	
	i2cval.buf[i2cval.count++] = val&0xff;
	
	for(i=0;i<loop;i++){
		ret =ioctl(fd, DRIVER_I2C_IO,&i2cval);	
		printf("%d= tx adapter(%d),addr(0x%x),reg(0x%x) val(0x%x)\n\r",ret,i2cval.adapter,i2cval.addr,reg,val);
	}

	
	close(fd);	
	return 0;
}

int cli_ipudump(parse_t * pars_p, char *result){
    int fd; 
	int ret;
	
	fd = open(DEVICE_NAME,O_RDWR);
	if(fd <=0){
		print("can't open fd %d \n\r",fd);
		return 0;
	}
	ret =ioctl(fd, DRIVER_DUMP_IPU_REGS,0);	
	close(fd);
	return 0;
}



	


int cli_regio(parse_t * pars_p, char *result){
    int fd; 
	int ret;
	unsigned int reg=0,val=-1;
	REGVAL_t regval ={0};
	fd = open(DEVICE_NAME,O_RDWR);
	if(fd <=0){
		print("can't open fd %d \n\r",fd);
		return 0;
	}

	cget_integer(pars_p,reg,(int*)&reg);
	cget_integer(pars_p,val,(int*)&val);
	regval.addr = reg;
	regval.val = val;
	
	if(val ==-1){
		ret =ioctl(fd, DRIVER_READ_REG32,&regval);	
		printf("reg(0x%8x) val(0x%8x) \n\r",regval.addr,regval.val);
	}
	else{
		ret =ioctl(fd, DRIVER_WRITE_REG32,&regval);	
		printf("reg(0x%8x) val(0x%8x) \n\r",regval.addr,regval.val);
	}	
	
	close(fd);
	return 0;
}


int cli_regdump(parse_t * pars_p, char *result){
    int fd; 
	int ret;
	int i;
	unsigned int reg=0,count=10;
	REGVAL_t regval ={0};
	fd = open(DEVICE_NAME,O_RDWR);
	if(fd <=0){
		print("can't open fd %d \n\r",fd);
		return 0;
	}

	cget_integer(pars_p,reg,(int*)&reg);
	cget_integer(pars_p,count,(int*)&count);
	for(i=0;i<count;i++){
	  regval.addr = reg + i * 4;
	  ret =ioctl(fd, DRIVER_READ_REG32,&regval);	
	  printf("reg(0x%8x) val(0x%8x) \n\r",regval.addr,regval.val);
	}
	
	close(fd);
	return 0;
}

int cli_spireg(parse_t * pars_p, char *result){
int fd; 
	int ret;
	unsigned int reg=0,val=0;
	REGVAL_t regval ={0};
	fd = open(DEVICE_NAME,O_RDWR);
	if(fd <=0){
		print("can't open fd %d \n\r",fd);
		return 0;
	}

	cget_integer(pars_p,reg,(int*)&reg);
	cget_integer(pars_p,val,(int*)&val);
	regval.addr = reg;
	regval.val = val;
	
	ret =ioctl(fd, DRIVER_REG_WRITE_SPI,&regval);	
	printf("reg(0x%8x) val(0x%8x) \n\r",regval.addr,regval.val);
	
	close(fd);
	return 0;
}

int sleep_i2c(int time){
  return 0;
}

int i2c_write(unsigned char reg,unsigned char *val){
    int fd; 
	int ret;
	I2CVAL_t i2cval={0};
//	int reg;
//	int val;
	fd = open(DEVICE_NAME,O_RDWR);
	if(fd <=0){
		print("can't open fd %d \n\r",fd);
		return 0;
	}
	
	i2cval.adapter =0;
	i2cval.addr    =0x21;
	
	if(reg >255){
		i2cval.buf[i2cval.count++] = reg>>8;
		i2cval.buf[i2cval.count++] = reg&0xff;
	}
	else{
		i2cval.buf[i2cval.count++] = reg&0xff;
	}
	
	i2cval.buf[i2cval.count++] = val[0];
	ret =ioctl(fd, DRIVER_I2C_IO,&i2cval);	
	fprintf(stderr,"i2c %d,0x%x [0x%x]=0x%x C(%d)\n\r",i2cval.adapter ,i2cval.addr,i2cval.buf[0],i2cval.buf[1],i2cval.count);
	close(fd);	
	return 0;
}


int cli_gsens(parse_t * pars_p, char *result){
    int fd; 
	int ret;
	int regval;
	
	cget_integer(pars_p,0xa00,(int*)&regval);
	fd = open(DEVICE_NAME,O_RDWR);
	if(fd <=0){
		print("can't open fd %d \n\r",fd);
		return 0;
	}

	ret =ioctl(fd, DRIVER_GSENS_CONF,&regval);	
	printf("sens_conf = 0x%x\n\r",regval);
	close(fd);	
	return 0;
}


void GpioTestDebug(void){
	register_command ("gpio"		,cli_gpio ,"<gpio> <val>");
	register_command ("gpiodir"		,cli_gpiodir ,"<dir> 0=input,1=output <defval> ");
	register_command ("gpiolist"	,cli_gpio_list ,"<start> <count>");
	register_command ("I2C"			,cli_i2ctest,"<addpter> <add> <reg> <val>");
	register_command ("I2Cloop"		,cli_i2cloop,"<#loop> <addpter> <addr> <reg> <val>");
	register_command ("ipudump"		,cli_ipudump,"");
	register_command ("regio"		,cli_regio,"<physc addr> <val>");
	register_command ("regdump"     ,cli_regdump,"<physc addr> <count>");
	register_command ("spireg"      ,cli_spireg,"<reg> <val>");	
	register_command ("advi2c"      ,cli_advi2c,"");
	register_command ("gsens",		cli_gsens,"<val>");
	
}





int main(int argc, char *argv[])
{
    int ret;
	
    printf("gpiotest main\n");

	sigemptyset(&mask);
	sigaddset(&mask, SIGINT);

	ret = pthread_create(&sig_thread_id, NULL, sig_thread, NULL);
	if (ret < 0) {
		printf("Create signal monitor thread Error!\n");
		return -1;
	}
	
	system("insmod /mx51driver.ko");	
	 
	
	TesttoolInit();
    //WindowsRegisterDebug();
    GpioTestDebug();
	
	TesttoolRun();
	system("rmmod mx51driver.ko");	
	
	
    return 0;
}




int cli_advi2c(parse_t * pars_p, char *result){
	int r;	
	unsigned char data[2];
	int sleep_time=0;

	//r = i2c_read(0x0, data, 252);
	sleep_i2c(sleep_time);

	data[0] = 0x09;
	r = i2c_write(0x0, data);
	sleep_i2c(sleep_time);
	data[0] = 0xC8;
	r = i2c_write(0x01, data);
	sleep_i2c(sleep_time);
	data[0] = 0x04;
	r = i2c_write(0x2, data);
	sleep_i2c(sleep_time);
	data[0] = 0x0;
	r = i2c_write(0x3, data);
	sleep_i2c(sleep_time);
	data[0] = 0x45;
	r = i2c_write(0x4, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x5, data);
	sleep_i2c(sleep_time);
	data[0] = 0x02;
	r = i2c_write(0x6, data);
	sleep_i2c(sleep_time);
	data[0] = 0x7F;
	r = i2c_write(0x7, data);
	sleep_i2c(sleep_time);
	data[0] = 0x80;
	r = i2c_write(0x08, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x0A, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x0B, data);
	sleep_i2c(sleep_time);
	data[0] = 0x36;
	r = i2c_write(0x0C, data);
	sleep_i2c(sleep_time);
	data[0] = 0x7C;
	r = i2c_write(0x0D, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x0E, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x0F, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x13, data);
	sleep_i2c(sleep_time);
	data[0] = 0x12;
	r = i2c_write(0x14, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x15, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x16, data);
	sleep_i2c(sleep_time);
	data[0] = 0x01;
	r = i2c_write(0x17, data);
	sleep_i2c(sleep_time);
	data[0] = 0x93;
	r = i2c_write(0x18, data);
	sleep_i2c(sleep_time);
	data[0] = 0x19;
	r = i2c_write(0xF1, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x1A, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x1B, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x1C, data);
	sleep_i2c(sleep_time);
	data[0] = 0x40;
	r = i2c_write(0x1D, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x1E, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x1F, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x20, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x21, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x22, data);
	sleep_i2c(sleep_time);
	data[0] = 0xC0;
	r = i2c_write(0x23, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x24, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x25, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x26, data);
	sleep_i2c(sleep_time);
	data[0] = 0x58;
	r = i2c_write(0x27, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x28, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x29, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x2A, data);
	sleep_i2c(sleep_time);
	data[0] = 0xE1;
	r = i2c_write(0x2B, data);
	sleep_i2c(sleep_time);
	data[0] = 0xAE;
	r = i2c_write(0x2C, data);
	sleep_i2c(sleep_time);
	data[0] = 0xF4;
	r = i2c_write(0x2D, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x2E, data);
	sleep_i2c(sleep_time);
	data[0] = 0xF0;
	r = i2c_write(0x2F, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x30, data);
	sleep_i2c(sleep_time);
	data[0] = 0x12;
	r = i2c_write(0x31, data);
	sleep_i2c(sleep_time);
	data[0] = 0x41;
	r = i2c_write(0x32, data);
	sleep_i2c(sleep_time);
	data[0] = 0x84;
	r = i2c_write(0x33, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x34, data);
	sleep_i2c(sleep_time);
	data[0] = 0x02;
	r = i2c_write(0x35, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x36, data);
	sleep_i2c(sleep_time);
	data[0] = 0x01;
	r = i2c_write(0x37, data);
	sleep_i2c(sleep_time);
	data[0] = 0x80;
	r = i2c_write(0x38, data);
	sleep_i2c(sleep_time);
	data[0] = 0xC0;
	r = i2c_write(0x39, data);
	sleep_i2c(sleep_time);
	data[0] = 0x10;
	r = i2c_write(0x3A, data);
	sleep_i2c(sleep_time);
	data[0] = 0x05;
	r = i2c_write(0x3B, data);
	sleep_i2c(sleep_time);
	data[0] = 0x58;
	r = i2c_write(0x3C, data);
	sleep_i2c(sleep_time);
	data[0] = 0xB2;
	r = i2c_write(0x3D, data);
	sleep_i2c(sleep_time);
	data[0] = 0x64;
	r = i2c_write(0x3E, data);
	sleep_i2c(sleep_time);
	data[0] = 0xE4;
	r = i2c_write(0x3F, data);
	sleep_i2c(sleep_time);
	data[0] = 0x90;
	r = i2c_write(0x40, data);
	sleep_i2c(sleep_time);
	data[0] = 0x01;
	r = i2c_write(0x41, data);
	sleep_i2c(sleep_time);
	data[0] = 0x7E;
	r = i2c_write(0x42, data);
	sleep_i2c(sleep_time);
	data[0] = 0xA4;
	r = i2c_write(0x43, data);
	sleep_i2c(sleep_time);
	data[0] = 0xFF;
	r = i2c_write(0x44, data);
	sleep_i2c(sleep_time);
	data[0] = 0xB6;
	r = i2c_write(0x45, data);
	sleep_i2c(sleep_time);
	data[0] = 0x12;
	r = i2c_write(0x46, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x48, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x49, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x4A, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x4B, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x4C, data);
	sleep_i2c(sleep_time);
	data[0] = 0xEF;
	r = i2c_write(0x4D, data);
	sleep_i2c(sleep_time);
	data[0] = 0x08;
	r = i2c_write(0x4E, data);
	sleep_i2c(sleep_time);
	data[0] = 0x08;
	r = i2c_write(0x4F, data);
	sleep_i2c(sleep_time);
	data[0] = 0x08;
	r = i2c_write(0x50, data);
	sleep_i2c(sleep_time);
	data[0] = 0x24;
	r = i2c_write(0x51, data);
	sleep_i2c(sleep_time);
	data[0] = 0x0B;
	r = i2c_write(0x52, data);
	sleep_i2c(sleep_time);
	data[0] = 0x4E;
	r = i2c_write(0x53, data);
	sleep_i2c(sleep_time);
	data[0] = 0x80;
	r = i2c_write(0x54, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x55, data);
	sleep_i2c(sleep_time);
	data[0] = 0x10;
	r = i2c_write(0x56, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x57, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x58, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x59, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x5A, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x5B, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x5C, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x5D, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x5E, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x5F, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x60, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x61, data);
	sleep_i2c(sleep_time);
	data[0] = 0x20;
	r = i2c_write(0x62, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x63, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x64, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x65, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x66, data);
	sleep_i2c(sleep_time);
	data[0] = 0x03;
	r = i2c_write(0x67, data);
	sleep_i2c(sleep_time);
	data[0] = 0x01;
	r = i2c_write(0x68, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x69, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x6A, data);
	sleep_i2c(sleep_time);
	data[0] = 0xC0;
	r = i2c_write(0x6B, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x6C, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x6D, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x6E, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x6F, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x70, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x71, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x72, data);
	sleep_i2c(sleep_time);
	data[0] = 0x10;
	r = i2c_write(0x73, data);
	sleep_i2c(sleep_time);
	data[0] = 0x04;
	r = i2c_write(0x74, data);
	sleep_i2c(sleep_time);
	data[0] = 0x01;
	r = i2c_write(0x75, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x76, data);
	sleep_i2c(sleep_time);
	data[0] = 0x3F;
	r = i2c_write(0x77, data);
	sleep_i2c(sleep_time);
	data[0] = 0xFF;
	r = i2c_write(0x78, data);
	sleep_i2c(sleep_time);
	data[0] = 0xFF;
	r = i2c_write(0x79, data);
	sleep_i2c(sleep_time);
	data[0] = 0xFF;
	r = i2c_write(0x7A, data);
	sleep_i2c(sleep_time);
	data[0] = 0x1E;
	r = i2c_write(0x7B, data);
	sleep_i2c(sleep_time);
	data[0] = 0xC0;
	r = i2c_write(0x7C, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x7D, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x7E, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x7F, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x80, data);
	sleep_i2c(sleep_time);
	data[0] = 0xC0;
	r = i2c_write(0x81, data);
	sleep_i2c(sleep_time);
	data[0] = 0x04;
	r = i2c_write(0x82, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x83, data);
	sleep_i2c(sleep_time);
	data[0] = 0x0C;
	r = i2c_write(0x84, data);
	sleep_i2c(sleep_time);
	data[0] = 0x02;
	r = i2c_write(0x85, data);
	sleep_i2c(sleep_time);
	data[0] = 0x03;
	r = i2c_write(0x86, data);
	sleep_i2c(sleep_time);
	data[0] = 0x63;
	r = i2c_write(0x87, data);
	sleep_i2c(sleep_time);
	data[0] = 0x5A;
	r = i2c_write(0x88, data);
	sleep_i2c(sleep_time);
	data[0] = 0x08;
	r = i2c_write(0x89, data);
	sleep_i2c(sleep_time);
	data[0] = 0x10;
	r = i2c_write(0x8A, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x8B, data);
	sleep_i2c(sleep_time);
	data[0] = 0x40;
	r = i2c_write(0x8C, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x8D, data);
	sleep_i2c(sleep_time);
	data[0] = 0x40;
	r = i2c_write(0x8E, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x8F, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x90, data);
	sleep_i2c(sleep_time);
	data[0] = 0x50;
	r = i2c_write(0x91, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x92, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x93, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x94, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x95, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x96, data);
	sleep_i2c(sleep_time);
	data[0] = 0xF0;
	r = i2c_write(0x97, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x98, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x99, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x9A, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x9B, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x9C, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x9D, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x9E, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0x9F, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xA0, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xA1, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xA2, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xA3, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xA4, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xA5, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xA6, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xA7, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xA8, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xA9, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xAA, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xAB, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xAC, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xAD, data);
	sleep_i2c(sleep_time);
	data[0] = 0x60;
	r = i2c_write(0xAE, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xAF, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xB0, data);
	sleep_i2c(sleep_time);
	data[0] = 0x60;
	r = i2c_write(0xB1, data);
	sleep_i2c(sleep_time);
	data[0] = 0x1C;
	r = i2c_write(0xB2, data);
	sleep_i2c(sleep_time);
	data[0] = 0x54;
	r = i2c_write(0xB3, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xB4, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xB5, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xB6, data);
	sleep_i2c(sleep_time);
	data[0] = 0x13;
	r = i2c_write(0xB7, data);
	sleep_i2c(sleep_time);
	data[0] = 0x03;
	r = i2c_write(0xB8, data);
	sleep_i2c(sleep_time);
	data[0] = 0x33;
	r = i2c_write(0xB9, data);
	sleep_i2c(sleep_time);
	data[0] = 0x02;
	r = i2c_write(0xBF, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xC0, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xC1, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xC2, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xC3, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xC4, data);
	sleep_i2c(sleep_time);
	data[0] = 0x81;
	r = i2c_write(0xC5, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xC6, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xC7, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xC8, data);
	sleep_i2c(sleep_time);
	data[0] = 0x04;
	r = i2c_write(0xC9, data);
	sleep_i2c(sleep_time);
	data[0] = 0x69;
	r = i2c_write(0xCC, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xCD, data);
	sleep_i2c(sleep_time);
	data[0] = 0x01;
	r = i2c_write(0xCE, data);
	sleep_i2c(sleep_time);
	data[0] = 0xB4;
	r = i2c_write(0xCF, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xD0, data);
	sleep_i2c(sleep_time);
	data[0] = 0x10;
	r = i2c_write(0xD1, data);
	sleep_i2c(sleep_time);
	data[0] = 0xFF;
	r = i2c_write(0xD2, data);
	sleep_i2c(sleep_time);
	data[0] = 0xFF;
	r = i2c_write(0xD3, data);
	sleep_i2c(sleep_time);
	data[0] = 0x7F;
	r = i2c_write(0xD4, data);
	sleep_i2c(sleep_time);
	data[0] = 0x7F;
	r = i2c_write(0xD5, data);
	sleep_i2c(sleep_time);
	data[0] = 0x3E;
	r = i2c_write(0xD6, data);
	sleep_i2c(sleep_time);
	data[0] = 0x08;
	r = i2c_write(0xD7, data);
	sleep_i2c(sleep_time);
	data[0] = 0x3C;
	r = i2c_write(0xD8, data);
	sleep_i2c(sleep_time);
	data[0] = 0x08;
	r = i2c_write(0xD9, data);
	sleep_i2c(sleep_time);
	data[0] = 0x3C;
	r = i2c_write(0xDA, data);
	sleep_i2c(sleep_time);
	data[0] = 0x9B;
	r = i2c_write(0xDB, data);
	sleep_i2c(sleep_time);
	data[0] = 0xAC;
	r = i2c_write(0xDC, data);
	sleep_i2c(sleep_time);
	data[0] = 0x4C;
	r = i2c_write(0xDD, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xDE, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xDF, data);
	sleep_i2c(sleep_time);
	data[0] = 0x14;
	r = i2c_write(0xE0, data);
	sleep_i2c(sleep_time);
	data[0] = 0x80;
	r = i2c_write(0xE1, data);
	sleep_i2c(sleep_time);
	data[0] = 0x80;
	r = i2c_write(0xE2, data);
	sleep_i2c(sleep_time);
	data[0] = 0x80;
	r = i2c_write(0xE3, data);
	sleep_i2c(sleep_time);
	data[0] = 0x80;
	r = i2c_write(0xE4, data);
	sleep_i2c(sleep_time);
	data[0] = 0x25;
	r = i2c_write(0xE5, data);
	sleep_i2c(sleep_time);
	data[0] = 0x44;
	r = i2c_write(0xE6, data);
	sleep_i2c(sleep_time);
	data[0] = 0x63;
	r = i2c_write(0xE7, data);
	sleep_i2c(sleep_time);
	data[0] = 0x65;
	r = i2c_write(0xE8, data);
	sleep_i2c(sleep_time);
	data[0] = 0x14;
	r = i2c_write(0xE9, data);
	sleep_i2c(sleep_time);
	data[0] = 0x63;
	r = i2c_write(0xEA, data);
	sleep_i2c(sleep_time);
	data[0] = 0x55;
	r = i2c_write(0xEB, data);
	sleep_i2c(sleep_time);
	data[0] = 0x55;
	r = i2c_write(0xEC, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xEE, data);
	sleep_i2c(sleep_time);
	data[0] = 0x4A;
	r = i2c_write(0xEF, data);
	sleep_i2c(sleep_time);
	data[0] = 0x44;
	r = i2c_write(0xF0, data);
	sleep_i2c(sleep_time);
	data[0] = 0x0C;
	r = i2c_write(0xF1, data);
	sleep_i2c(sleep_time);
	data[0] = 0x32;
	r = i2c_write(0xF2, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xF3, data);
	sleep_i2c(sleep_time);
	data[0] = 0x15;
	r = i2c_write(0xF4, data);
	sleep_i2c(sleep_time);
	data[0] = 0xE0;
	r = i2c_write(0xF5, data);
	sleep_i2c(sleep_time);
	data[0] = 0x69;
	r = i2c_write(0xF6, data);
	sleep_i2c(sleep_time);
	data[0] = 0x10;
	r = i2c_write(0xF7, data);
	sleep_i2c(sleep_time);
	data[0] = 0x00;
	r = i2c_write(0xF8, data);
	sleep_i2c(sleep_time);
	data[0] = 0x03;
	r = i2c_write(0xF9, data);
	sleep_i2c(sleep_time);
	data[0] = 0xFA;
	r = i2c_write(0xFA, data);
	sleep_i2c(sleep_time);
	data[0] = 0x40;
	r = i2c_write(0xFB, data);
	return 0;
}
